package com.sdvdxl.service.impl;

import java.util.Date;
import java.util.List;

import com.sdvdxl.mapper.BlogMapper;
import com.sdvdxl.mapper.BlogTagMapper;
import com.sdvdxl.mapper.BlogTagUnionMapper;
import com.sdvdxl.model.BlogEntity;
import com.sdvdxl.model.BlogTagEntity;
import com.sdvdxl.model.BlogTagUnionEntity;
import com.sdvdxl.model.BlogTypeEntity;
import com.sdvdxl.model.PaggerEntity;
import com.sdvdxl.service.BlogService;
import com.sdvdxl.util.ApplicationData;
import com.sdvdxl.util.BlogHandler;
import com.sdvdxl.util.IdGernater;
import com.sdvdxl.util.JsonResult;

public class BlogServiceImpl implements BlogService {
	private BlogMapper blogMapper;
	private BlogTagMapper blogTagMapper;
	private BlogTagUnionMapper blogTagUnionMapper;

	public BlogTagUnionMapper getBlogTagUnionMapper() {
		return blogTagUnionMapper;
	}

	public void setBlogTagUnionMapper(BlogTagUnionMapper blogTagUnionMapper) {
		this.blogTagUnionMapper = blogTagUnionMapper;
	}

	public BlogTagMapper getBlogTagMapper() {
		return blogTagMapper;
	}

	public void setBlogTagMapper(BlogTagMapper blogTagMapper) {
		this.blogTagMapper = blogTagMapper;
	}

	public BlogMapper getBlogMapper() {
		return blogMapper;
	}

	public void setBlogMapper(BlogMapper blogMapper) {
		this.blogMapper = blogMapper;
	}

	@Override
	public JsonResult add(BlogEntity blogEntity) {
		JsonResult result = new JsonResult();
		String id = IdGernater.uuid();
		blogEntity.setId(id);
		blogEntity.setBlogId(id);
		Date date = new Date();
		blogEntity.setCreateTime(date);
		blogEntity.setLastEditTime(date);
		// 查找标签，如果标签存在，则直接引用，否则先插入标签在引用
		String[] tags = blogEntity.getBlogTagsStr().split("\\s");
		for (String tag : tags) {
			List<BlogTagEntity> blogTags = blogTagMapper.getByName(tag);
			BlogTagEntity tmpTag = null;
			if (blogTags.isEmpty()) {
				tmpTag = new BlogTagEntity();
				tmpTag.setId(IdGernater.uuid());
				tmpTag.setName(tag);
				blogTagMapper.add(tmpTag);// 添加标签到tag表
			} else {
				tmpTag = blogTags.get(0);
			}
			BlogTagUnionEntity btu = new BlogTagUnionEntity();
			btu.setBlogEntity(blogEntity);
			btu.setBlogTagEntity(tmpTag);
			blogTagUnionMapper.add(btu);
		}
		blogEntity = new BlogHandler(blogEntity).saveBlog();
		blogMapper.add(blogEntity);// 添加到blog表
		//更新application中的标签
		ApplicationData.getInstance().updateBlogTags();
		result.setMsg("添加成功");
		result.setSuccess(true);
		return result;
	}

	@Override
	public JsonResult del(BlogEntity blogEntity) {
		JsonResult result = new JsonResult();
		return result;

	}

	@Override
	public BlogEntity get(BlogEntity blogEntity) {
		return blogMapper.get(blogEntity);
	}

	@Override
	public List<BlogEntity> getAll() {
		return blogMapper.getAll();
	}

	@Override
	public void update(BlogEntity blogEntity) {
		// TODO Auto-generated method stub

	}

	@Override
	public List<BlogEntity> getBlogsByType(BlogTypeEntity blogTypeEntity) {
		return blogMapper.getBlogsByType(blogTypeEntity);
	}

	@Override
	public List<BlogEntity> getBlogsByTag(BlogTagEntity blogTagEntity) {
		return blogMapper.getBlogsByTag(blogTagEntity);
	}

	@Override
	public List<BlogEntity> getBlogsByTypeWithPagger(PaggerEntity<BlogEntity> paggerEntity) {
		return blogMapper.getBlogsByTypeWithPagger(paggerEntity);
	}

}
